<?php
namespace app\helpers;

use Yii;

/**
 *  分页工具
 */
class Paging{
    
    /**
     * 分页函数
     * @param int $curpage : 当前页
     * @param int $pagesize 
     * @param int $total
     * @param string $urlParams
     * @return string
     */
    public static function paging($curpage,$pagesize,$total){
        
        $host = Yii::$app->request->url;
        $pageurl = Yii::$app->request->getHostInfo().$host;
        $pageurlExt = '';
        //url的链接符号   & ?
        $urlSign = strpos($pageurl,'?') !== false ? '&' : '?';
        $sign = '';
    
        //页面展示的分页链接数目
        $pageLinks = 5;
        $totalPages = (int)(($total + $pagesize - 1) / $pagesize);
        $curpage = max(intval($curpage),1);
    
        $pstart = $pend = 0;
        //需要展示全部页数链接
        if($totalPages <= $pageLinks){
    
            $pstart = 1;
            $pend = $totalPages;
    
        }else{
    
            $halfLinks = (int)($pageLinks/2);
            //当前页显示在左边
            if( ($curpage - $halfLinks) <= 0 ){
                $pstart = 1;
                $pend = $pageLinks;
    
                //当前页显示在右边
            }else if( ($curpage + $halfLinks) > $totalPages){
    
                $pstart = $totalPages - $pageLinks + 1;
                $pend = $totalPages;
    
                //当前页面显示在页面中间
            }else{
                $pstart = $curpage - $halfLinks;
                $pend = $curpage + $halfLinks;
            }
        }
    
        //左右省略号
        $dotLeft = $dotRight = '';
        $dotLeft = $pstart > 1 ? '...' : '';
        $dotRight = $pend < $totalPages ? '...' : '';
    
        $links = [];  //要显示的页数
        if($pstart <= $pend ){
            for($i = $pstart;$i <= $pend; $i++){
                $links[] = $i;
            }
        }
    
        //处理掉
        $pageurl = preg_replace('/[\&|\?]page=\d*/i','',$pageurl);
        //$pageurl = preg_replace('/\?page=\d*/i','',$pageurl);
        $urlSign = strpos($pageurl,'?') !== false ? '&' : '?';
    
        $page['url_sign'] = $urlSign;
        //上一页，下一页
        $page['last_page'] = max($curpage - 1,1);
        $page['next_page'] = min($totalPages,$curpage + 1);
        $page['last_url'] = $pageurl . $urlSign . 'page='. $page['last_page'];
        $page['next_url'] = $pageurl . $urlSign . 'page='. $page['next_page'];
        //左右省略号
        $page['dot_left'] = $dotLeft;
        $page['dot_right'] = $dotRight;
    
        //总条目
        $page['total'] = $total;
        //总页数
        $page['total_pages'] = $totalPages;
        $page['current_page'] = $curpage;
        //要展示的链接页， 2  3  4  5  6
        $page['page_link_num'] = $links;
        $page['page_url'] = $pageurl ? $pageurl : $host."/index.php";
        return $page;
    
    }
    
    /**
     * 接口统一返回的分页数据处理
     * @param array $data
     */
    public static function apiPaging($data){
        
        $pinfo = $data['page'];
        $curpage = $pinfo['currentPage'];
        $pagesize = $pinfo['pageSize'];
        $total = $pinfo['totalCount'];
        $paging = self::paging($curpage, $pagesize, $total);
        
        return Yii::$app->view->renderFile('@app/views/public/paging.php',['paging' => $paging]);
        
    }
    
    /**
     * 后台数据库操作返回数据分页
     * @param array $data
     */
    public static function dbPaging($data,$page,$pagesize,$total){
    
        $paging = self::paging($page, $pagesize, $total);
    
        return Yii::$app->view->renderFile('@app/views/public/paging.php',['paging' => $paging]);
    
    }
    
}
